package com.swt.gap.service;

import com.swt.gap.dao.*;
import com.swt.gap.model.entity.TaskFileModel;
import com.swt.gap.model.entity.TaskLabels;
import com.swt.gap.model.entity.TaskRelationshipType;
import com.swt.gap.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 请填写说明
 *
 * @author sx
 * @date 2020/7/21 11:39
 */
@Component
@Slf4j
public class TaskGraphData {
    private final TaskLabelsDao taskLabelsDao;
    private final TaskRelationshipTypeDao taskRelationshipTypeDao;
    private final TaskFileModelMapper taskFileModelMapper;
    private final TaskFileMapper taskFileMapper;
    private final RedisUtil redisUtil;

    public TaskGraphData(TaskLabelsDao taskLabelsDao,TaskRelationshipTypeDao taskRelationshipTypeDao,
                         TaskFileModelMapper taskFileModelMapper,
                         TaskFileMapper taskFileMapper,
                         RedisUtil redisUtil) {
        this.taskLabelsDao = taskLabelsDao;
        this.taskRelationshipTypeDao = taskRelationshipTypeDao;
        this.taskFileModelMapper = taskFileModelMapper;
        this.taskFileMapper = taskFileMapper;
        this.redisUtil = redisUtil;
    }

    @Async
    public void addTaskLabels(Integer taskId, Set<List<String>> taskLabels) {
        taskLabels.forEach(labels -> {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < labels.size(); i++) {
                sb.append(labels.get(i));
                if (i < labels.size() - 1) {
                    sb.append(",");
                }
            }
            taskLabelsDao.insertNotExists(new TaskLabels(taskId, sb.toString()));
        });
    }

    @Async
    public Integer addTaskRelationshipType(Integer taskId, String relationshipType, Integer ontologyRelationshipId) {

        return taskRelationshipTypeDao.insertNotExists(new TaskRelationshipType(taskId, relationshipType, ontologyRelationshipId));
    }


    @Async
    public Integer addTaskFileModel(Integer taskId, Integer fileId, String modelPicContent) {
        TaskFileModel taskFileModel = new TaskFileModel();
        taskFileModel.setTaskId(taskId);
        taskFileModel.setFileId(fileId);
        taskFileModel.setModelPicContent(modelPicContent);

        return taskFileModelMapper.insertSelective(taskFileModel);
    }

    @Async
    public Integer updateStatus(Integer taskId, Integer fileId, Integer status) {
        return taskFileMapper.updateStatus(fileId, taskId, status);
    }

    @Async
    public void saveToRedis(String key, Object value) {
        redisUtil.set(key, value);
    }
}
